import linkListDefine.ListNode;
public class exchangeListNode {
    public ListNode exchangeNode(ListNode start){
        ListNode head = new ListNode(-1,start);//虚拟头结点，方便返回新链表
        ListNode list = head;
        while (list.next!=null&&list.next.next!=null){
            ListNode temp = list.next.next.next;//保存交换结点的下一个结点
            ListNode pre = list.next;//交换结点的前一个
            ListNode cur = list.next.next;//交换结点的后一个
            list.next =  cur;
            cur.next = pre;//交换结点位置
            list = pre;
            list.next = temp;//将虚拟头结点移动到pre的位置
        }
        return head.next;
    }
    public static void main(String[] args) {
        var main = new exchangeListNode();
        int[] list = new int[]{1,2,3,4,5,6,7};
        //建立链表，从后往前进行建立链表
        ListNode a = new ListNode(list[list.length-1]);
        for(int i=list.length-2;i>=0;i--){
            ListNode b = new ListNode(list[i],a);
            a = b;
        }
        ListNode res = main.exchangeNode(a);
        while (res!=null){
            System.out.println(res.data);
            res = res.next;
        }
    }
}
